package com.example.sherlockzhong.plush_v2.Components.switchs;

/*
 * Created by DosonLeung on 2014/8/25.
 */

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;

import com.example.sherlockzhong.plush_v2.CardIni.AddOrganizationIni;
import com.example.sherlockzhong.plush_v2.Constants.SystemConstant;
import com.example.sherlockzhong.plush_v2.Models.PushModel;
import com.example.sherlockzhong.plush_v2.R;
import com.example.sherlockzhong.plush_v2.Subscriptions.AddOrganizationActivity;
import com.example.sherlockzhong.plush_v2.Utils.NetworkUtils;

public class AddOrgSwitch extends Switch {
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
         /**
         * 0x123表示关注或取消关注成功
         * 0x101表示回复选择
         * 0x200表示刷新成功
         * */
         if (msg.what == 0x123) {
            //stopReflashDialog();
            AddOrganizationIni.refreshEvent(apps.refresh,apps,false);
         }else if(msg.what == 0x101){
            stopReflashDialog();
         }else if(msg.what == 0x200){

         }
        }
    };
    private ProgressDialog pd;
    private Context context;
    private Object id;
    private boolean isSelected;
    private boolean userSelected = false;
    private AddOrganizationActivity apps;

    public AddOrgSwitch(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public AddOrgSwitch(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public AddOrganizationActivity getApps() {
        return apps;
    }

    public void setApps(AddOrganizationActivity apps) {
        this.apps = apps;
    }

    public Object getID() {
        return id;
    }

    public void setID(Object id) {
        this.id = id;
    }

    public void init() {
        this.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (apps == null) {
                return;
            } else if (NetworkUtils.getAvailableNetWorkInfo(apps) == null) {
                return;
            }
            if (isChecked) {
                //选中时 do some thing
                selected(context, id);
                isSelected = true;
            } else {
                //非选中时 do some thing
                if (isSelected)
                    unselected(context, id);
            }
            SystemConstant.updateOrgs = true;
            }
        });
    }

    private void selected(final Context context, Object id) {
        final String orgID = id.toString();
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                boolean ok = false;
                //showReflashDialog("提交中...");
                apps.showReflashDialog("提交中...");
                ok = PushModel.subscribe(orgID, true);
                if (ok) {
                    handler.sendEmptyMessage(0x123);
                } else {
                    Toast.makeText(context, "抱歉，由于网络原因，关注失败", Toast.LENGTH_SHORT).show();
                }
                Looper.loop();
            }
        }.start();
    }

    private void unselected(final Context context, Object id) {
        final String orgID = id.toString();
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                boolean ok = false;
                apps.showReflashDialog("提交中...");
                ok = PushModel.subscribe(orgID, false);
                userSelected = false;
                handler.sendEmptyMessage(0x123);
                if (ok) {
                    handler.sendEmptyMessage(0x123);
                } else {
                    //handler.sendEmptyMessage(0x101);
                    Toast.makeText(context, "抱歉，由于网络原因，取消关注失败", Toast.LENGTH_SHORT).show();
                }
                Looper.loop();
            }
        }.start();
    }

    /**
     * 显示刷新的时候的等待进度条
     */
    public void showReflashDialog(String msg) {
        this.pd = new ProgressDialog(context);
        this.pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        this.pd.setCancelable(false);
        this.pd.setTitle(msg);
        this.pd.show();
    }

    /**
     * 结束刷新的时候的等待进度条
     */
    public void stopReflashDialog() {
        if (this.pd != null) {
            pd.dismiss();
        }
    }
}
